package com.example.taskschedule.infrastructure.threadPool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 自定义线程工厂
 */
public class CustomThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger threadNumber = new AtomicInteger(1); 
    public CustomThreadFactory(String namePrefix) {
        this.namePrefix = namePrefix;
    }
    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r, namePrefix + "-" + threadNumber.getAndIncrement());
        // 设置线程为非守护线程
        if(!t.isDaemon()){
            t.setDaemon(false);
        }
        // 设置线程优先级
        if(t.getPriority() != Thread.NORM_PRIORITY){
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}
